using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.Diagnostics;
namespace MicroMsg.Network
{
	public class FlowControl
	{
		private const int MAX_EXCOUNT_DAYS = 7;
		private static uint mLastTotalBytes = 0u;
		private static FlowCountInfoEx gFlowCountInfoExMap;
		private static int mCurrentDayNumber = 0;
		private static object flowLockObject = new object();
		public static void initialize()
		{
			FlowControl.checkDate();
			FlowControl.gFlowCountInfoExMap = ConfigMgr.read<FlowCountInfoEx>();
			if (FlowControl.gFlowCountInfoExMap == null)
			{
				FlowControl.gFlowCountInfoExMap = new FlowCountInfoEx();
				FlowControl.gFlowCountInfoExMap.mTotalInfo.mStartupTime = string.Concat(FlowControl.mCurrentDayNumber);
			}
			FlowControl.mLastTotalBytes = FlowControl.gFlowCountInfoExMap.mTotalInfo.mBytesTotal;
		}
		private static double nowToSeconds()
		{
			DateTime dateTime = DateTime.get_Now().ToLocalTime();
			DateTime dateTime2 = new DateTime(1970, 1, 1, 0, 0, 0);
			return dateTime.Subtract(dateTime2).get_TotalSeconds();
		}
		private static DateTime sencondsToDate(long seconds)
		{
			DateTime result = new DateTime(1970, 1, 1, 0, 0, 0);
			DateTime now;
			try
			{
				result = result.AddSeconds((double)seconds);
				return result;
			}
			catch
			{
				now = DateTime.get_Now();
			}
			return now;
		}
		private static void checkDate()
		{
			int num = (int)(FlowControl.nowToSeconds() / 86400.0);
			if (FlowControl.mCurrentDayNumber == 0)
			{
				FlowControl.mCurrentDayNumber = num;
				return;
			}
			if (FlowControl.mCurrentDayNumber == num)
			{
				return;
			}
			FlowControl.flushFlowSettings();
			FlowControl.mCurrentDayNumber = num;
		}
		public static void printInfo()
		{
		}
		public static void printInfoByPassedDay(int count)
		{
		}
		private static void checkInit()
		{
			FlowControl.checkDate();
		}
		public static void cleanCount()
		{
		}
		public static string dotFormat(uint number)
		{
			string text = number.ToString("n");
			return text.Substring(0, text.get_Length() - 3);
		}
		public static void printfFlowInfo(FlowCountInfo flowInfo)
		{
		}
		public static void printfInfo(CmdFlow cmdFlow)
		{
		}
		private static void checkSaveSettings()
		{
			lock (FlowControl.flowLockObject)
			{
				if (FlowControl.gFlowCountInfoExMap != null)
				{
					if (FlowControl.gFlowCountInfoExMap.mTotalInfo.mBytesTotal > FlowControl.mLastTotalBytes + 307200u || Debugger.get_IsAttached())
					{
						FlowControl.mLastTotalBytes = FlowControl.gFlowCountInfoExMap.mTotalInfo.mBytesTotal;
						ConfigMgr.write<FlowCountInfoEx>(FlowControl.gFlowCountInfoExMap);
					}
				}
			}
		}
		public static void flushFlowSettings()
		{
			lock (FlowControl.flowLockObject)
			{
				if (FlowControl.gFlowCountInfoExMap != null)
				{
					if (FlowControl.mLastTotalBytes != FlowControl.gFlowCountInfoExMap.mTotalInfo.mBytesTotal)
					{
						FlowControl.mLastTotalBytes = FlowControl.gFlowCountInfoExMap.mTotalInfo.mBytesTotal;
						ConfigMgr.write<FlowCountInfoEx>(FlowControl.gFlowCountInfoExMap);
					}
				}
			}
		}
		public static bool onSend(int int_0, string cmdUri, uint uint_0)
		{
			FlowControl.checkInit();
			FlowControl.addFlowExItem(uint_0);
			FlowControl.checkSaveSettings();
			return true;
		}
		public static bool onReceive(int int_0, string cmdUri, uint uint_0)
		{
			FlowControl.checkInit();
			FlowControl.addFlowExItem(uint_0);
			FlowControl.checkSaveSettings();
			return true;
		}
		private static bool isWlanFlow()
		{
			return NetworkDeviceWatcher.isCurrentWifi() || (!NetworkDeviceWatcher.isCurrentCell3G() && !NetworkDeviceWatcher.isCurrentCell2G() && NetworkDeviceWatcher.isWifiAvailable());
		}
		private static void addSendItem(uint uint_0, FlowCountInfo flowInfo, bool isWlan)
		{
			if (isWlan)
			{
				flowInfo.mBytesWlanUp += uint_0;
			}
			else
			{
				flowInfo.mBytesCellularUp += uint_0;
			}
			flowInfo.mBytesTotal += uint_0;
		}
		private static void addRecvItem(uint uint_0, FlowCountInfo flowInfo, bool isWlan)
		{
			if (isWlan)
			{
				flowInfo.mBytesWlanDown += uint_0;
			}
			else
			{
				flowInfo.mBytesCellularDown += uint_0;
			}
			flowInfo.mBytesTotal += uint_0;
		}
		public static void onNetFlowWithPassedDay(uint uint_0, bool isWlan, long secondsTime)
		{
			FlowControl.checkInit();
			long num = secondsTime / 86400L;
			if (num + 7L < (long)FlowControl.mCurrentDayNumber)
			{
				return;
			}
			if (num > (long)FlowControl.mCurrentDayNumber)
			{
				return;
			}
			FlowControl.addFlowExItemByDay(uint_0, (int)num, isWlan);
			FlowControl.checkSaveSettings();
		}
		private static void addFlowExItem(uint uint_0)
		{
			FlowControl.addFlowExItemByDay(uint_0, FlowControl.mCurrentDayNumber, FlowControl.isWlanFlow());
		}
		private static void addFlowExItemByDay(uint uint_0, int dayCount, bool isWlan)
		{
			lock (FlowControl.flowLockObject)
			{
				if (FlowControl.gFlowCountInfoExMap != null)
				{
					FlowControl.addSendItem(uint_0, FlowControl.gFlowCountInfoExMap.mTotalInfo, isWlan);
					int num = dayCount % 7;
					if (!FlowControl.gFlowCountInfoExMap.mapFlowCountInfo.ContainsKey(num))
					{
						FlowCountInfo flowCountInfo = new FlowCountInfo();
						flowCountInfo.mStartupTime = string.Concat(dayCount);
						FlowControl.addSendItem(uint_0, flowCountInfo, isWlan);
						FlowControl.gFlowCountInfoExMap.mapFlowCountInfo.Add(num, flowCountInfo);
					}
					else
					{
						FlowCountInfo flowCountInfo2 = FlowControl.gFlowCountInfoExMap.mapFlowCountInfo.get_Item(num);
						if (flowCountInfo2.mStartupTime != string.Concat(dayCount))
						{
							flowCountInfo2.reset();
							flowCountInfo2.mStartupTime = string.Concat(dayCount);
						}
						FlowControl.addSendItem(uint_0, flowCountInfo2, isWlan);
					}
				}
			}
		}
		public static void cleanNetStream()
		{
			lock (FlowControl.flowLockObject)
			{
				if (FlowControl.gFlowCountInfoExMap != null)
				{
					FlowControl.gFlowCountInfoExMap.mTotalInfo.reset();
					FlowControl.gFlowCountInfoExMap.mapFlowCountInfo.Clear();
					FlowControl.gFlowCountInfoExMap.mTotalInfo.mStartupTime = string.Concat(FlowControl.mCurrentDayNumber);
					FlowControl.addFlowExItem(0u);
					ConfigMgr.write<FlowCountInfoEx>(FlowControl.gFlowCountInfoExMap);
					FlowControl.mLastTotalBytes = FlowControl.gFlowCountInfoExMap.mTotalInfo.mBytesTotal;
				}
			}
		}
		private static NetStreamItem getStreamItemFromFlowInfo(FlowCountInfo flowInfo)
		{
			return new NetStreamItem
			{
				startDate = FlowControl.sencondsToDate((long)(Util.stringToInt(flowInfo.mStartupTime) * 24 * 3600)),
				totalStreamCell = flowInfo.mBytesCellularUp * 104u / 100u,
				totalStreamWifi = flowInfo.mBytesWlanUp * 104u / 100u
			};
		}
		public static NetStreamItem getTotalNetStream()
		{
			NetStreamItem result;
			lock (FlowControl.flowLockObject)
			{
				if (FlowControl.gFlowCountInfoExMap == null)
				{
					result = null;
				}
				else
				{
					result = FlowControl.getStreamItemFromFlowInfo(FlowControl.gFlowCountInfoExMap.mTotalInfo);
				}
			}
			return result;
		}
		public static NetStreamItem getNetStreamByDayOffset(int offset)
		{
			lock (FlowControl.flowLockObject)
			{
				if (FlowControl.gFlowCountInfoExMap == null)
				{
					NetStreamItem result = null;
					return result;
				}
				int num = FlowControl.mCurrentDayNumber % 7;
				if (offset > 0)
				{
					NetStreamItem result = null;
					return result;
				}
				offset = -offset;
				if (offset >= 7)
				{
					NetStreamItem result = null;
					return result;
				}
				num = (num + 7 - offset) % 7;
				if (FlowControl.gFlowCountInfoExMap.mapFlowCountInfo.ContainsKey(num))
				{
					FlowCountInfo flowCountInfo = FlowControl.gFlowCountInfoExMap.mapFlowCountInfo.get_Item(num);
					NetStreamItem result;
					if (flowCountInfo.mStartupTime != string.Concat(FlowControl.mCurrentDayNumber - offset))
					{
						result = null;
						return result;
					}
					result = FlowControl.getStreamItemFromFlowInfo(FlowControl.gFlowCountInfoExMap.mapFlowCountInfo.get_Item(num));
					return result;
				}
			}
			return null;
		}
	}
}
